package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.FollowedTagAddRequest;
import com.frogs.ucenter.v1.request.FollowedTagDeleteRequest;
import com.frogs.ucenter.v1.request.FollowedTagEditRequest;
import com.frogs.ucenter.v1.request.TagUserRelationListRequest;
import com.frogs.ucenter.v1.request.UserTagAddRequest;
import com.frogs.ucenter.v1.request.UserTagDeleteRequest;
import com.frogs.ucenter.v1.request.UserTagEditRequest;
import com.frogs.ucenter.v1.service.TagUserRelationService;
import com.frogs.ucenter.v1.service.UserBaseInfoService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "tagUserRelationController")
@RequestMapping("/" + ApiConstants.VERSION )
public class TagUserRelationController extends BaseController3 {

	@Resource(name = ApiConstants.VERSION + "tagUserRelationService")
	private TagUserRelationService tagUserRelationService;

	@Resource(name=ApiConstants.VERSION + "userBaseInfoService")
	private UserBaseInfoService userBaseInfoService;
	
	private static final String ROOT = "/tag";
	private static final String TAG_USER_LIST = ROOT + "/user/list";

	private static final String TAG_USER_DELETE = ROOT + "/user" + DELETE;
	private static final String TAG_FOLLOWED_DELETE = ROOT + "/followed" + DELETE;
    
    private static final String TAG_USER_ADD = ROOT + "/user" + ADD;
    private static final String TAG_FOLLOWED_ADD = ROOT + "/followed" + ADD;
    
    private static final String TAG_USER_EDIT = ROOT + "/user" + EDIT;
    private static final String TAG_FOLLOWED_EDIT = ROOT + "/followed" + EDIT;
    
	
    
	@RequestMapping(TAG_USER_LIST)
	public Response tagUserList(HttpServletRequest httpRequest) {
		Request request = new TagUserRelationListRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = tagUserRelationService.listUserTags(request);
			response.setTip("关联成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}

		return response;
	}
	

	@RequestMapping({TAG_USER_DELETE})
	public Response tagUserDelete(HttpServletRequest request) {
		UserTagDeleteRequest utdr = new UserTagDeleteRequest();
		Response response;
		try {
			utdr.parse(request);
			response = userBaseInfoService.deleteUserTag(utdr);
			response.setTip("操作成功");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	@RequestMapping({TAG_FOLLOWED_DELETE})
	public Response tagFollowedDelete(HttpServletRequest request) {
		FollowedTagDeleteRequest followedTagDeleteRequest = new FollowedTagDeleteRequest();
		Response response;
		try {
			followedTagDeleteRequest.parse(request);
			response = userBaseInfoService.deleteFollowedTag(followedTagDeleteRequest);
			response.setTip("操作成功");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		return response;
	}
	
	@RequestMapping({TAG_USER_ADD})
	public Response tagUserAdd(HttpServletRequest httpRequest) {
		Request request = new UserTagAddRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userBaseInfoService.addUserTag(request);
			
			response.setTip("添加用户标签成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}

		return response;
	}
	
	@RequestMapping({TAG_FOLLOWED_ADD})
	public Response tagFollowedAdd(HttpServletRequest httpRequest) {
		Request request = new FollowedTagAddRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userBaseInfoService.addFollowedTag(request);
			
			response.setTip("添加未知用户标签成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		
		return response;
	}
	
	@RequestMapping({TAG_USER_EDIT})
	public Response tagUserEdit(HttpServletRequest httpRequest) {
		Request request = new UserTagEditRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userBaseInfoService.editUserTag(request);
			
			response.setTip("编辑用户标签成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}

		return response;
	}
	
	@RequestMapping({TAG_FOLLOWED_EDIT})
	public Response tagFollowedEdit(HttpServletRequest httpRequest) {
		Request request = new FollowedTagEditRequest();
		Response response;
		try {
			request.parse(httpRequest);
			response = userBaseInfoService.editFollowedTag(request);
			
			response.setTip("编辑未知用户标签成功！");
		} catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
		
		return response;
	}
	
}